{extend name="base" /}
<!-- page specific plugin styles -->
{block name="plugin-styles"}{/block}
<!-- inline styles related to this page -->
{block name="inline-styles"}
<link rel="stylesheet" href="/static/ace/assets/css/bootstrap-datepicker3.min.css" />
<style type="text/css">
.mybtn{
    margin-left: 5px !important;
}
</style>
{/block}
<!-- breadcrumb -->
{block name="breadcrumbs"}
<ul class="breadcrumb">
    <li>
        <i class="ace-icon fa fa-home home-icon"></i>
        <a href="{:url('Index/index')}">首页</a>
    </li>
    <li class="active">商城管理</li>
</ul>
<!-- /.breadcrumb -->
{/block}
<!-- PAGE CONTENT BEGINS -->
{block name="content"}
<div class="page-header">
    <h1>
		订单列表
	</h1>
</div>
<!-- /.page-header -->
<div class="clearfix tableTools-container">
    <form class="pull-left form-inline" id='searchForm'>
    <input type="hidden" class="form-control input-sm" name="mid" value='{$mid}' >
     <div class="form-group">
                    <div class="input-daterange input-group"  style="width:300px" >
                        <input type="text" class="input-sm form-control" id='startTime' name="startTime" data-date-format="yyyy-mm-dd" placeholder="开始时间" autocomplete = "off"/>
                        <span class="input-group-addon"><i class="fa fa-exchange"></i></span>
                        <input type="text" class="input-sm form-control"  id='endTime'name="endTime" data-date-format="yyyy-mm-dd" placeholder="结束时间" autocomplete = "off"/>
                    </div>
        </div>
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-addon">代理名称</span>
                <input type="text" class="form-control input-sm" name="name" placeholder="代理姓名或手机号" autocomplete = "off">
            </div>
        </div>
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-addon">收货人</span>
                <input type="text" class="form-control input-sm" name="receive_name" placeholder="收货人名称" autocomplete = "off">
            </div>
        </div>
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-addon">手机号</span>
                <input type="text" class="form-control input-sm" name="tel" placeholder="收货人手机号" autocomplete = "off">
            </div>
        </div>
        <div class="space-2"></div>
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-addon">产品名称</span>
                <input type="text" class="form-control input-sm" name="goodsname" placeholder="产品名称" autocomplete = "off">
            </div>
        </div>
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-addon">产品ID</span>
                <input type="text" class="form-control input-sm" name="goodsid" placeholder="产品ID" autocomplete = "off">
            </div>
        </div>
        <div class="form-group">
        	<label class="control-label" for="checkbox">订单状态 : </label>
        	<div class="input-group">
        		<div class="checkbox">
					<label>
						<input name="status" type="checkbox" {eq name='status' value='1'}checked {/eq} class="ace" value='1'>
						<span class="lbl">未审核</span>
					</label>
					<label>
						<input name="status" type="checkbox" {eq name='status' value='2'}checked {/eq} class="ace" value='2'>
						<span class="lbl">已取消</span>
					</label>
					<label>
						<input name="status" type="checkbox" {eq name='status' value='3'}checked {/eq} class="ace" value='3'>
						<span class="lbl">已发货</span>
					</label>
					<label>
						<input name="status" type="checkbox" {eq name='status' value='4'}checked {/eq} class="ace" value='4'>
						<span class="lbl">已退货</span>
					</label>
					<label>
						<input name="status" type="checkbox" {eq name='status' value='5'}checked {/eq} class="ace" value='5'>
						<span class="lbl">已完成</span>
					</label>
				</div>
        	</div>
        </div>
        
        <div class="space-2">
        </div>
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-addon">订单编号</span>
                <input type="text" class="form-control input-sm" name="oid" placeholder="请输入订单编号" autocomplete = "off">
            </div>
        </div>
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-addon">物流单号</span>
                <input type="text" class="form-control input-sm" name="express_no" placeholder="请输入物流单号" autocomplete = "off">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label" for="ispay">订单状态</label>
            <select class="form-control input-sm" name="ispay" id="">
                <option value="0">全部</option>
                <option value="1" {eq name='status' value='1'}selected {/eq}>未审核</option>
                <option value="2" {eq name='status' value='2'}selected {/eq}>已取消</option>
                <option value="3" {eq name='status' value='3'}selected {/eq}>已发货</option>
                <option value="4" {eq name='status' value='4'}selected {/eq}>已退货</option>
                <option value="5" {eq name='status' value='5'}selected {/eq}>已完成</option>
            </select>
        </div>
        <div class="form-group">
            <div class="input-group">
	            <label class="input-group-addon" for="flog">是否导出</label>
	            <select class="form-control input-sm" name="flag" id="">
	                <option value="">全部</option>
	                <option value="0">未导出</option>
	                <option value="1">已导出</option>
	            </select>
            </div>
        </div>
        
        <div class="form-group">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <button type="button" class="btn btn-xs btn-primary" id='searchTable'>搜索</button>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <button type="reset" class="btn btn-xs btn-primary" >重置</button>
            &nbsp;&nbsp;
            <button type="button" class="btn btn-xs btn-primary" id='export' onclick="downlaod()">导出</button>
        </div>
    </form>
    <div class="pull-right">
        <a href="{:url('order/upload')}" target='_blank' class="btn btn-xs btn-primary">上传物流信息</a>
        &nbsp;&nbsp;&nbsp;&nbsp;
    </div>
    
</div>
<div class="table-responsive">
	<table class="table table-bordered" id="DynamicTable"></table>
</div>
<!-- /.table-responsive -->

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header no-padding">
                <div class="table-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    物流信息
                </div>
            </div>
            <div class="modal-body no-padding">
                <form id="ajaxForm">
                    <input type="hidden" name="id">
                    <input type="hidden" name="func">
                    <table class="table table-bordered no-margin-bottom no-border-top">
                        <tbody>
                            <tr>
                                <th class="text-right" width="20%">物流公司</th>
                                <td>
                                    <input type="text" class="form-control input-sm" name="express_name" >
                                </td>
                            </tr>
                            <tr>
                                <th class="text-right">物流号</th>
                                <td><input type="text" class="form-control input-sm" name="express_no" ></td>
                            </tr>
                        </tbody>
                    </table>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-sm btn-success" id="showToast"> <i class="ace-icon fa fa-check"></i> save </button>
                <button class="btn btn-sm btn-danger pull-right" data-dismiss="modal"> <i class="ace-icon fa fa-times"></i> Close </button>
            </div>
        </div>
    </div>
</div>
{/block}
<!-- PAGE CONTENT ENDS -->
<!-- page specific plugin scripts -->
{block name="plugin-scripts"}
<script src="/static/ace/assets/js/jquery.dataTables.min.js"></script>
<script src="/static/ace/assets/js/jquery.dataTables.bootstrap.min.js"></script>
<script src="/static/ace/assets/js/moment.min.js"></script>
<script src="/static/ace/assets/js/bootstrap-datepicker.min.js"></script>
 <script src="/static/ace/assets/js/daterangepicker.min.js"></script>
 <script src="/static/ace/assets/js/bootstrap-datetimepicker.min.js"></script>
{/block}
<!-- inline scripts related to this page -->
{block name="related-scripts"}
<script type="text/javascript">
	var myTable = 
	$('#DynamicTable')
	.wrap("<div class='dataTables_borderWrap' />")   //if you are applying horizontal scrolling (sScrollX)
	.DataTable({
		"autoWidth": false,	//智能列宽处理
		"deferRender": true,	//延迟渲染以获得额外的初始化速度
		"info": true,	//表信息显示
		"lengthChange": false,	//更改每页记录数
		"ordering": false,	//排序功能
		"paging": true,	//启用或禁用表分页
		"processing": false,	//处理指标
		// "scrollX": false,	//水平滚动
		// "scrollY": false,	//垂直滚动
		"searching": false,	//搜索功能
		"serverSide": true,	//服务器端处理模式
		"stateSave": false,	//状态保存(页面重新加载时恢复表状态)
		"language": {
            "url": "//cdn.datatables.net/plug-ins/1.10.19/i18n/Chinese.json"
        },
		"ajax": {
            "url": "/admin/order/rs",
            "type": "POST",
            "data": function (d) {
            	var status = new Array();
            	$("input[name='status']:checked").each(function () {
                    return status.push(this.value);
                })
                console.log(status)
                
                d.name = $("input[name=name]").val();
                d.startTime = $("input[name=startTime]").val();
                d.endTime = $("input[name=endTime]").val();
                d.oid = $("input[name=oid]").val();
                d.receive_name = $("input[name=receive_name]").val();
                d.tel = $("input[name=tel]").val();
                d.express_no = $("input[name=express_no]").val();
                d.status =status.join();
                d.ispay =$("select[name=ispay]").val();
                d.flag = $("select[name=flag]").val();;/* $("select[name=status]").val(); */
                d.mid=$("input[name=mid]").val();
                d.goodsname=$("input[name=goodsname]").val();
                d.goodsid=$("input[name=goodsid]").val();
            },
            "dataSrc": function (json) {
                return json.data;
            }
        },
        "columns": [{
            "title": "订单编号",
            "data": "oid"
        }, {
            "title": "代理",
            "data": "name"
        }, {
            "title": "级别",
            "data": "level"
        }, {
            "title": "产品名称",
            "data": "goods_name"
        }, {
            "title": "数量",
            "data": "num"
        }, {
            "title": "单价",
            "data": "goods_price"
        }, {
            "title": "订单总价",
            "data": "total"
        }, {
            "title": "订单状态",
            "data": "status"
        }, {
            "title": "是否导出(物流)",
            "data": "flag"
        },{
            "title": "下单时间",
            "data": "ctime"
        },{
            "title": "收货人",
            "data": "receive_name"
        },{
            "title": "收货人手机号",
            "data": "tel"
        },{
            "title": "仓库",
            "data": "store_name"
        },{
            "title": "操作",
            "data": "id"
        }],
        "columnDefs": [{
        	"className": "text-center",
        	"targets": [0,1,2,4,5,6,7,8,9,10,11,12]
        },{
            "render": function (data, type, row, meta) {
                return '<a href="{:url('agent/detail')}?mid='+row.mid+'" target="  _blank">'+row.name+'</a>';;
            },
            "targets": 1  
        },
        {
            "render": function (data, type, row, meta) {
                
                if (data == 1) {
                    return '<span class="label arrowed arrowed-right label-warning">'+row.status_name+'</span>';
                } else if (data == 2) {
                    return '<span class="label arrowed arrowed-right label-danger">'+row.status_name+'</span>';
                } else if (data == 3) {
                    return '<span class="label arrowed arrowed-right label-inverse">'+row.status_name+'</span>';
                }else if (data == 4) {
                    return '<span class="label arrowed arrowed-right label-info">'+row.status_name+'</span>';
                }else if (data == 5) {
                    return '<span class="label arrowed arrowed-right label-success">'+row.status_name+'</span>';
                }
                
            },
            "targets": 7   
        },
        {
            "render": function (data, type, row, meta) {
                
            	if (data == 1) {
                    return '<span class="label label-success">已导出</span>';
                } else if (data == 0) {
                    return '<span class="label label-warning">未导出</span>';
                }
                return '-';
            },
            "targets": 8
        }
        , {
        	"render": function (data, type, row, meta) {
        		var pass = '';
        		var unpass = '';
        		var express = '';
        		var refund = '';
        		var finish = '';
        		
        		if(row.status == 1){
        			pass = '<button type="button" data-toggle="modal" data-target="#myModal" class="btn btn-xs btn-primary mybtn" onClick="orderSend('+data+')">通过</button>'
        			unpass ='<button type="button" class="btn btn-xs btn-danger mybtn" onClick="isDelete('+data+')">不通过</button>' 
        		}
        		if(row.status == 3){
        			express = '<button type="button" data-toggle="modal" data-target="#myModal" class="btn btn-xs btn-primary mybtn" onClick="updateExpress('+data+')">修改物流</button>'
        			refund = '<button type="button" class="btn btn-xs btn-success mybtn" onClick="orderRefund('+data+')">退款</button>'
        			finish = '<button type="button" class="btn btn-xs btn-success mybtn" onClick="orderFinish('+data+')">完成</button>'
                }
        		
        		
                return '<div class="btn-group">\
                	<a target="_blank" class="btn btn-xs btn-info mybtn" href="{:url('order/detail')}?id='+data+'">查看详情</a>'+
                	pass + express + unpass + refund+ finish
                    +'</div>';
            },
            "targets": 13
        }],
        "initComplete": function( settings, json ) {
        	$("#DynamicTable").prev(".row").remove();
        	//add tooltip for small view
			$('[data-rel="tooltip"]').tooltip();
		}
	});

    $("#searchTable").click(function () {
        myTable.draw();
    });
    

    function downlaod(){
        var name = $("input[name=nane]").val();
        var startTime = $("input[name=startTime]").val();
        var endTime = $("input[name=endTime]").val();
        var status = $("select[name=ispay]").val();
        var flag = $("select[name=flag]").val();
        var order_no = $("input[name=oid]").val();
        var tel = $("input[name=tel]").val();
        var downloadForm = $("<form action=\"/admin/order/export\" method=\"post\" style=\"display: none;\"></form>");
        var startTimeInput = $("<input type=\"text\" name=\"startTime\" value=\"" + startTime + "\" />");
        var endTimeInput = $("<input type=\"text\" name=\"endTime\" value=\"" + endTime + "\" />");
        var statusInput = $("<input type=\"text\" name=\"status\" value=\"" + status + "\" />");
        var orderNoInput = $("<input type=\"text\" name=\"oid\" value=\"" + order_no + "\" />");
        var telInput = $("<input type=\"text\" name=\"tel\" value=\"" + tel + "\" />");
        var flagInput = $("<input type=\"text\" name=\"flag\" value=\"" + flag + "\" />");
        downloadForm.append(startTimeInput).append(endTimeInput).append(statusInput).append(orderNoInput).append(telInput).append(flagInput);
        $("body").append(downloadForm);
        downloadForm.submit();
        downloadForm.remove();
    }
    
    //发货
    $("#showToast").click(function(event) {
        /* Act on the event */
        
        $func = $("#ajaxForm").find('input[name=func]').val();
        url = "/admin/order/send";
        if($func == 'express'){
        	url = "/admin/order/update_express";
        }
        var options={
            url:url,
            type:"POST",
            dataType:"json",
            success: function(json){
                if(json.returnCode==1){
                    alert(json.returnMsg);
                    $("#myModal").modal('hide');
                    myTable.draw();
                }else{
                    alert(json.returnMsg);
                }
            }
        };
        $("#ajaxForm").ajaxSubmit(options);
        $("#ajaxForm").find('input[name=express_no]').val('');
    });
    function orderSend(_id) {
        // body...
        
        $("#ajaxForm").find('input[name=id]').val(_id);
        $("#ajaxForm").find('input[name=func]').val('send');
    }
    
    // 删除
    function isDelete(_id){
        var _istTrue = confirm("确认取消该订单？");
        if (_istTrue==true) {
            $.ajax({
                url: '/admin/order/cancel',
                type: 'POST',
                dataType: 'json',
                data: {id: _id},
            })
            .done(function(json) {
                console.log("success");
                if(json.returnCode==1){
                    alert(json.returnMsg);
                    myTable.draw();
                }else{
                    alert(json.returnMsg);
                }
            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                console.log("complete");
            });
            
        }else {
            // console.log("You pressed Cancel!")
        }
        
    }
    
    
  //完成
    function orderRefund(_id) {
        // body...
    	var msg = "确定要退款吗？请确认！"; 
        if (confirm(msg)!=true){ 
            return false; 
        } 
        $.ajax({
            url: '/admin/order/refund',
            type: 'POST',
            dataType: 'json',
            data: {id: _id},
        })
        .done(function(json) {
            console.log("success");
            if(json.returnCode==1){
                alert('操作成功');
                myTable.draw();
            }else{
                alert(json.returnMsg);
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
        
    }

    //完成
    function orderFinish(_id) {
        // body...
    	var msg = "确定要设置为完成吗？请确认！"; 
        if (confirm(msg)!=true){ 
            return false; 
        } 
        $.ajax({
            url: '/admin/order/finish',
            type: 'POST',
            dataType: 'json',
            data: {id: _id},
        })
        .done(function(json) {
            console.log("success");
            if(json.returnCode==1){
                alert('操作成功');
                myTable.draw();
            }else{
                alert(json.returnMsg);
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
        
    }

    function updateExpress(_id){
    	// body...
        $("#ajaxForm").find('input[name=id]').val(_id);
        $("#ajaxForm").find('input[name=func]').val('express');
    }
    $('#startTime').datepicker({
        autoclose:true
    });
    $('#endTime').datepicker({
        autoclose:true
    });

</script>
{/block}